# -*- coding: utf-8 -*-
"""
Created on Wed Apr 17 10:21:38 2024

@author: 刘翼
"""

import cv2
img = cv2.imread(r"C:\Users\Public\opencv\Figure\bird.jpg")
#二值化阈值处理
ret,thresh1=cv2.threshold(img,127,255,cv2.THRESH_BINARY)  
# 反二值化阈值处理
ret,thresh2=cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
#截断阈值处理
ret,thresh3=cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
#低阈值零处理
ret,thresh4=cv2.threshold(img,127,255,cv2.THRESH_TOZERO)  
# 超阈值零处理
ret,thresh5=cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV) 
# cv2.imshow('origin_img', img)
# cv2.imshow('thresh1_img', thresh1)
# cv2.imshow('thresh2_img', thresh2)
# cv2.imshow('thresh3_img', thresh3)
# cv2.imshow('thresh4_img', thresh4)
# cv2.imshow('thresh5_img', thresh5)

ret1,th_img = cv2.threshold(img,50,255,cv2.THRESH_BINARY)  # 全局阈值

# Otsu's阈值处理彩色图像
img_b, img_g ,img_r = cv2.split(img)        #通道分离
ret2_b,Oth_b = cv2.threshold(img_b,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)  
ret2_g,Oth_g = cv2.threshold(img_g,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)  
ret2_r,Oth_r = cv2.threshold(img_r,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)  
Otsu_img = cv2.merge([Oth_b,Oth_g,Oth_r])  #通道合并
print("全局阈值 =",ret1)
print("BGR三个通道Ostu's阈值 =", ret2_b, ret2_g, ret2_r)

cv2.imshow('Origin_img', img)
cv2.imshow('Binary_img', th_img)
cv2.imshow('Otsu"s_img', Otsu_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
